package src.netty.rpc.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import src.netty.rpc.provider.OrderServiceImpl;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-06-13
 */
public class MyNettyServerHandler extends ChannelInboundHandlerAdapter {


    private static final String PROTOCOL = "dubbo#";


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("msg：" + msg);
        String mssage = msg.toString();
        if (mssage.startsWith(PROTOCOL)) {
            String order = new OrderServiceImpl().getOrder(Long.valueOf(mssage.substring(mssage.lastIndexOf("#") + 1)));
            ctx.writeAndFlush(order);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
